﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SHomeWorkshop.LunarPresentation.Dialogs
{
    /// <summary>
    /// Interaction logic for BaseTextEditor.xaml
    /// </summary>
    public partial class BaseTextEditor : Window
    {
        public BaseTextEditor()
        {
            InitializeComponent();
            inputTextBox.ContextMenu = null;
            inputTextBox.Focus();
            Globals.SwitchInputMethod(true);

            if (Globals.selectedEditor != null)
            {
                if (Globals.selectedEditor.DefaultBackground == Brushes.Transparent)
                {
                    outBorder.Background = Brushes.WhiteSmoke;
                }
                else
                {
                    outBorder.Background = Globals.selectedEditor.DefaultBackground;
                }

                editorTitleBorder.BorderBrush =
                editorTitle.Foreground = Globals.selectedEditor.DefaultForeground;
            }

            InputMethod.SetIsInputMethodEnabled(inputTextBox, true);

            //给边框添加变色效果，结果发现有打扰人的反面效果，于是改成只闪动两次。
            Brush myBrush = new SolidColorBrush();

            ColorAnimation myColorAnimation = new ColorAnimation();
            if (Globals.selectedEditor != null &&
                Globals.selectedEditor.ExSelectionAdornerBrush is SolidColorBrush)
            {
                myColorAnimation.From = ((SolidColorBrush)Globals.selectedEditor.ExSelectionAdornerBrush).Color;
            }
            else
            {
                myColorAnimation.From = Colors.Red;
            }

            myColorAnimation.To = Colors.Transparent;
            myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200));
            myColorAnimation.AutoReverse = true;
            myColorAnimation.RepeatBehavior = new RepeatBehavior(2);

            //将动画应用到画刷（Brush）的“Color”属性。
            myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myColorAnimation);
            outBorder.BorderBrush = myBrush;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            inputTextBox.SelectedText = (sender as Button).Content.ToString();
            inputTextBox.SelectionStart += 1;
            inputTextBox.SelectionLength = 0;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            Globals.SwitchInputMethod(false);

            this.Close();
        }

        private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            bool isShift = false;

            KeyboardDevice kd = e.KeyboardDevice;

            if ((kd.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightShift) & KeyStates.Down) > 0)
            {
                isShift = true;
            }

            if (e.Key == Key.Enter)
            {
                if (isShift == false)
                {
                    btnOk_Click(sender, e);
                }
                else
                {
                    this.inputTextBox.SelectedText = "\r\n";
                    this.inputTextBox.SelectionLength = 0;
                    this.inputTextBox.SelectionStart += 2;
                }
            }
            else if (e.Key == Key.Escape)
            {
                this.DialogResult = false;

                Globals.SwitchInputMethod(false);

                this.Close();
            }
        }

        #region 透明窗口代码
        //protected override void OnSourceInitialized(EventArgs e)
        //{
        //    base.OnSourceInitialized(e);

        //    if (System.Environment.OSVersion.Version.Major >= 6)//Vista及更高版本的操作系统。
        //    {
        //        // 这可在 SourceInitialized 事件前完成:
        //        SHomeWorkshop.LunarPresentation.AeroGlassLib.AeroGlassForWPFWindow.ExtendGlassFrame(this,
        //            new SHomeWorkshop.LunarPresentation.AeroGlassLib.MARGINS(0, 0, 50, 28));

        //        IntPtr hwnd = new WindowInteropHelper(this).Handle;
        //        HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
        //    }
        //}

        //private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        //{
        //    if (msg == WM_DWMCOMPOSITIONCHANGED)
        //    {
        //        // 重启玻璃效果:
        //        SHomeWorkshop.LunarPresentation.AeroGlassLib.AeroGlassForWPFWindow.ExtendGlassFrame(this,
        //             new SHomeWorkshop.LunarPresentation.AeroGlassLib.MARGINS(0, 0, 50, 28));
        //        handled = true;
        //    }
        //    return IntPtr.Zero;
        //}

        //private const int WM_DWMCOMPOSITIONCHANGED = 0x031E;

        #endregion

        private void fontSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            inputTextBox.FontSize = fontSizeSlider.Value;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;

            Globals.SwitchInputMethod(false);

            this.Close();
        }

        private void inputTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            bool isCtrl = false;
            bool isShift = false;

            KeyboardDevice kd = e.KeyboardDevice;

            if ((kd.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightCtrl) & KeyStates.Down) > 0)
            {
                isCtrl = true;
            }

            if ((kd.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightShift) & KeyStates.Down) > 0)
            {
                isShift = true;
            }

            switch (e.Key)
            {
                case Key.Up:
                    {
                        if (isCtrl)
                        {
                            if (Globals.selectedEditor != null)
                            {
                                Globals.selectedEditor.BaseScrolledViewer.LineUp();
                            }
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.Down:
                    {
                        if (isCtrl)
                        {
                            if (Globals.selectedEditor != null)
                            {
                                Globals.selectedEditor.BaseScrolledViewer.LineDown();
                            }
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.Left:
                    {
                        if (isCtrl)
                        {
                            if (Globals.selectedEditor != null)
                            {
                                Globals.selectedEditor.BaseScrolledViewer.LineLeft();
                            }
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.Right:
                    {
                        if (isCtrl)
                        {
                            if (Globals.selectedEditor != null)
                            {
                                Globals.selectedEditor.BaseScrolledViewer.LineRight();
                            }
                            e.Handled = true;
                        }
                        break;
                    }
                case Key.V:
                    {
                        if (isCtrl && !isShift)
                        {
                            inputTextBox.SelectedText = Clipboard.GetText(TextDataFormat.UnicodeText);
                            inputTextBox.Select(inputTextBox.SelectionStart + inputTextBox.SelectionLength, 0);
                            e.Handled = true;
                        }
                        break;
                    }
            }
        }
    }
}
